#include "malbum.h"

MAlbum::MAlbum(QObject *parent) :
    QAbstractTableModel(parent)
{
    setObjectName("ModelAlbum");
}

MAlbum::~MAlbum()
{
}

Qt::ItemFlags MAlbum::flags(const QModelIndex& index) const
{
    Qt::ItemFlags theFlags = QAbstractTableModel::flags(index);
    if (index.isValid())
    {
        if(index.column() == DtAlbum::Name)
            theFlags |= Qt::ItemIsSelectable;
    }
    return theFlags;
}

QVariant MAlbum::data(const QModelIndex& index, int role) const
{
  if (!index.isValid() ||
      index.row() < 0 || index.row() >= mAlbum.size() ||
      index.column() < 0 || index.column() >= DtAlbum::NbAttr)
    return QVariant();
  DtAlbum *album = mAlbum[index.row()];
  if(role == Qt::DisplayRole || role == Qt::EditRole)
  {
      switch(index.column())
      {
      case DtAlbum::Play:
          return album->getPlay();
      case DtAlbum::Add:
          return album->getAdd();
      case DtAlbum::Name:
          return album->getName();
      case DtAlbum::Year:
          return album->getYear();
      case DtAlbum::Heart:
          return album->getHeartDelegate();
      default: ;
      }
  }
  if(role == Qt::ToolTipRole)
  {
      const QList<DtTrack *> lTrack = album->getTracks();
      return album->getName() + "\n Has : "
              + QString::number(lTrack.size()) + " tracks";
  }

  return QVariant();
}

QVariant MAlbum::headerData(int section, Qt::Orientation orientation,
                             int role) const
{
    if (role != Qt::DisplayRole)
        return QVariant();
    if (orientation == Qt::Horizontal && section < DtAlbum::NbAttr)
    {
        if(section == DtAlbum::Name)
            return QObject::tr("Name");
        else if(section == DtAlbum::Heart)
            return QObject::tr("Year");
    }
    return "";
}

int MAlbum::rowCount(const QModelIndex & parent) const
{
    return parent.isValid() ? 0 : mAlbum.size();
}

int MAlbum::columnCount(const QModelIndex & parent) const
{
    return parent.isValid() ? 0 : DtAlbum::NbAttr;
}

bool MAlbum::insertRows(int , int , const QModelIndex&)
{
    return false;
}

bool MAlbum::removeRows(int row, int count, const QModelIndex&)
{
  beginRemoveRows(QModelIndex(), row, row + count - 1);
  for (int i = 0; i < count; ++i)
    mAlbum.removeAt(row);
  endRemoveRows();
  return true;
}

QList<QHeaderView::ResizeMode> MAlbum::getHorizontalResizeModes() const
{
    QList<QHeaderView::ResizeMode> t;
    t.append(QHeaderView::Fixed);
    t.append(QHeaderView::Fixed);
    t.append(QHeaderView::Stretch);
    t.append(QHeaderView::Fixed);
    t.append(QHeaderView::Stretch);
  return t;
}

void MAlbum::setList(QList<DtAlbum *> &lAlbum)
{
    mAlbum = lAlbum;
    reset();
}

QList<DtAlbum *> MAlbum::getList() const
{
    return mAlbum;
}
